package com.example.zzj.ziwuyundemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzj.ziwuyundemo.R;
import com.example.zzj.ziwuyundemo.base.BaseActivity;
import com.example.zzj.ziwuyundemo.entity.ReplyEntity;
import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * Created by zzj on 2016/12/13.
 */

public class ReplyActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.img_info)
    ImageView imgInfo;
    @InjectView(R.id.name_waybills)
    TextView nameWaybills;
    @InjectView(R.id.linearLayout)
    LinearLayout linearLayout;
    @InjectView(R.id.carrierVehicle)
    TextView carrierVehicle;
    @InjectView(R.id.transportRoutes)
    TextView transportRoutes;
    @InjectView(R.id.supplier)
    TextView supplier;
    @InjectView(R.id.goods)
    TextView goods;
    @InjectView(R.id.goodsCategory)
    TextView goodsCategory;
    @InjectView(R.id.reply)
    Button reply;
    @InjectView(R.id.status)
    TextView status;
    private Intent intent;
    private String userId;
    private OkHttpClient okHttpClient;
    private String replyBody;
    private ReplyEntity replyEntity;
    Boolean up=false;//默认false不刷新
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reply);
        ButterKnife.inject(this);

        initView();
        initData();
    }

    private void initView() {
        back.setVisibility(View.VISIBLE);
        imgInfo.setVisibility(View.VISIBLE);
        nameWaybills.setVisibility(View.VISIBLE);

        intent = getIntent();
        userId = intent.getStringExtra("userId");
        Log.d("userId", userId);
        submit();

    }
    private void submit() {
        okHttpClient = new OkHttpClient();
        FormEncodingBuilder builder = new FormEncodingBuilder();
        builder.add("userId", userId);

        //超时
        okHttpClient.setConnectTimeout(10, TimeUnit.SECONDS);
        okHttpClient.setReadTimeout(10, TimeUnit.SECONDS);
        okHttpClient.setWriteTimeout(10, TimeUnit.SECONDS);

        final Request request = new Request.Builder()
                .url("http://192.168.1.75:8080/mclcm/a/mclcm/test/getpcd")
                .post(builder.build())
                .build();
        Log.d("request1", request.toString());
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                replyBody = response.body().string();
                Log.d("replyBody",replyBody);
                Gson gson = new Gson();
                replyEntity = new ReplyEntity();
                replyEntity = gson.fromJson(replyBody, ReplyEntity.class);
                if (replyEntity.isSui()== false){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(mContext,"没有派车单",Toast.LENGTH_SHORT).show();
                        }
                    });

                }else{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            carrierVehicle.setText(replyEntity.getRecodeSendcarPlans().getCarrierVehicle());
                            transportRoutes.setText(replyEntity.getRecodeSendcarPlans().getTransportRoutes().getName());
                            supplier.setText(replyEntity.getRecodeSendcarPlans().getSupplier());
                            goods.setText(replyEntity.getRecodeSendcarPlans().getGoods());
                            goodsCategory.setText(replyEntity.getRecodeSendcarPlans().getGoodsCategory());
                            switch (replyEntity.getRecodeSendcarPlans().getVbillState()){
                                case "0":
                                    status.setText("未响应");
                                    reply.setVisibility(View.VISIBLE);
                                    break;
                                case "1":
                                    status.setText("响应");
                                    reply.setVisibility(View.GONE);
                                    break;
                                case "2":
                                    status.setText("废弃");
                                    reply.setVisibility(View.GONE);
                                    break;
                                case "3":
                                    status.setText("完成");
                                    reply.setVisibility(View.GONE);
                                    break;
                            }
                        }
                    });
                }
            }
        });
    }


    private void initData() {
        back.setOnClickListener(this);
        reply.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.reply:
                intent = new Intent(mContext, DetailActivity.class);
                intent.putExtra("pcId", replyEntity.getRecodeSendcarPlans().getId());
                intent.putExtra("carrierVehicle", replyEntity.getRecodeSendcarPlans().getCarrierVehicle());
                intent.putExtra("transportRoutes", replyEntity.getRecodeSendcarPlans().getTransportRoutes().getName());
                intent.putExtra("supplier", replyEntity.getRecodeSendcarPlans().getSupplier());
                intent.putExtra("goods", replyEntity.getRecodeSendcarPlans().getGoods());
                intent.putExtra("goodsCategory", replyEntity.getRecodeSendcarPlans().getGoodsCategory());
                intent.putExtra("planCode", replyEntity.getRecodeSendcarPlans().getPlanCode());
                intent.putExtra("createDate", replyEntity.getRecodeSendcarPlans().getCreateDate());
                intent.putExtra("status",replyEntity.getRecodeSendcarPlans().getVbillState());
                intent.putExtra("carrierPlansId",replyEntity.getRecodeSendcarPlans().getCarrierplansId());
                startActivity(intent);
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        up =true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (up){
            submit();
            up = false;
        }
    }


}
